package com.sg.service.biz.component.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.component.req.*;
import com.sg.dto.biz.component.res.*;
import com.sg.service.biz.component.ConfSchemeService;
import com.sg.service.biz.component.nb.NbSystemDocking;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class SystemDockingServiceImpl
        implements com.wicket.okrcomponent.biz.service.SystemDockingService {

    @Resource
    private RedisUtil redisUtil;
    //@Resource
    //private MCustomFields4Service mCustomFields4Service;
    @Resource
    private NbSystemDocking nbSystemDocking;
    @Resource
    private ConfSchemeService confSchemeService;
//@Resource
    //private MCustomFields5Service mCustomFields5Service;

    /**
     * D2查询数据库配置答案(公共)[8753]
     * gen by moon at 3/23/2024, 7:30:20 PM
     */
    @Trace(operationName = "D2查询数据库配置答案(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryDatabaseConfAnswerDetailComRespDto queryDatabaseConfAnswerDetailCom(QueryDatabaseConfAnswerDetailComReqDto reqDto) {


        ImplementSmartSessionReceiveFieldInputRespDto receptionServiceRes_1 = null;
//virtualUsage D2查数据导入对象数据库配置答案  64617
        QueryConfItemMatchAnswerDetailComRespDto queryConfItemMatchAnswerDetailComRespDto = null;
        QueryConfItemMatchAnswerDetailComReqDto queryConfItemMatchAnswerDetailComReqDto = new QueryConfItemMatchAnswerDetailComReqDto();
        queryConfItemMatchAnswerDetailComReqDto.setConfItemCode("IMPORT_OBJECT_DATABASE_CONF");//CUSTOM_CONVENTION//sourceId:1639059_1_64617
        queryConfItemMatchAnswerDetailComReqDto.setConfSchemeCode("COM_CONF_SCHEME");//CUSTOM_CONVENTION//sourceId:1639062_1_64617
        queryConfItemMatchAnswerDetailComReqDto.setIsPlatData("TRUE");//sourceId:1639068_1_64617
        if (reqDto != null) {
            queryConfItemMatchAnswerDetailComReqDto.setConfOptionCode(reqDto.getBusinessObjectCode());//SimpleFieldAssign//sourceId:1639079_1_64617
        }

        /*D2查数据导入对象数据库配置答案[2486]   */
        Assert.isNull(queryConfItemMatchAnswerDetailComReqDto.getConfOptionCode(), "D2查询数据库配置答案(公共)-D2查数据导入对象数据库配置答案-选项标识不能为空", false);
        Assert.isNull(queryConfItemMatchAnswerDetailComReqDto.getConfItemCode(), "D2查询数据库配置答案(公共)-D2查数据导入对象数据库配置答案-配置项标识不能为空", false);
        Assert.isNull(queryConfItemMatchAnswerDetailComReqDto.getConfSchemeCode(), "D2查询数据库配置答案(公共)-D2查数据导入对象数据库配置答案-配置方案标识不能为空", false);
        Assert.isNull(queryConfItemMatchAnswerDetailComReqDto.getIsPlatData(), "D2查询数据库配置答案(公共)-D2查数据导入对象数据库配置答案-是否标准答案不能为空", false);
        queryConfItemMatchAnswerDetailComRespDto = confSchemeService.queryConfItemMatchAnswerDetailCom(queryConfItemMatchAnswerDetailComReqDto)/*vcase invoke isSameApp*/;
        Assert.isTrue(queryConfItemMatchAnswerDetailComRespDto == null || queryConfItemMatchAnswerDetailComRespDto.getEndValue() == null, "找不到数据，系统异常", false);


        if ((queryConfItemMatchAnswerDetailComRespDto != null && queryConfItemMatchAnswerDetailComRespDto.getEndValue() != null)) {
            //if(D2查数据导入对象数据库配置答案.答案值 值不等于空 )  64618

            QueryItemMatchResultOptionToAnswerDetailComRespDto queryItemMatchResultOptionToAnswerDetailComRespDto = null;
            if (queryConfItemMatchAnswerDetailComRespDto != null) {
                QueryItemMatchResultOptionToAnswerDetailComReqDto queryItemMatchResultOptionToAnswerDetailComReqDto = new QueryItemMatchResultOptionToAnswerDetailComReqDto();
                queryItemMatchResultOptionToAnswerDetailComReqDto.setConfSchemeCode("COM_CONF_SCHEME");//CUSTOM_CONVENTION//sourceId:1639053_1_64619
                queryItemMatchResultOptionToAnswerDetailComReqDto.setIsPlatData("TRUE");//sourceId:1639012_1_64619
                queryItemMatchResultOptionToAnswerDetailComReqDto.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:1639014_1_64619
                if (queryConfItemMatchAnswerDetailComRespDto != null) {
                    queryItemMatchResultOptionToAnswerDetailComReqDto.setConfItemCode(queryConfItemMatchAnswerDetailComRespDto.getEndValue());//SimpleFieldAssign//sourceId:1639009_1_64619
                }

                /*D2查平台数据库配置答案[5991]   */
                Assert.isNull(queryItemMatchResultOptionToAnswerDetailComReqDto.getConfItemCode(), "D2查询数据库配置答案(公共)-D2查平台数据库配置答案-配置项标识不能为空", false);
                Assert.isNull(queryItemMatchResultOptionToAnswerDetailComReqDto.getConfSchemeCode(), "D2查询数据库配置答案(公共)-D2查平台数据库配置答案-配置方案标识不能为空", false);
                Assert.isNull(queryItemMatchResultOptionToAnswerDetailComReqDto.getIsPlatData(), "D2查询数据库配置答案(公共)-D2查平台数据库配置答案-是否标准答案不能为空", false);
                Assert.isNull(queryItemMatchResultOptionToAnswerDetailComReqDto.getSubjectLifeCycle(), "D2查询数据库配置答案(公共)-D2查平台数据库配置答案-主体生命周期不能为空", false);
                queryItemMatchResultOptionToAnswerDetailComRespDto = confSchemeService.queryItemMatchResultOptionToAnswerDetailCom(queryItemMatchResultOptionToAnswerDetailComReqDto)/*vcase invoke isSameApp*/;


            }
//ModelCode: receptionService
            ImplementSmartSessionReceiveFieldInputRespDto receptionServiceRes = null;
            if (queryItemMatchResultOptionToAnswerDetailComRespDto != null) {
                ImplementSmartSessionReceiveFieldInputReqDto receptionServiceReq = new ImplementSmartSessionReceiveFieldInputReqDto();
                if (queryItemMatchResultOptionToAnswerDetailComRespDto != null) {
                    receptionServiceReq.setDatabaseName(queryItemMatchResultOptionToAnswerDetailComRespDto.getDatabaseName());//SimpleFieldAssign//sourceId:1639395_1_64620
                    receptionServiceReq.setDatabaseAddress(queryItemMatchResultOptionToAnswerDetailComRespDto.getDatabaseAddress());//SimpleFieldAssign//sourceId:1639391_1_64620
                    receptionServiceReq.setDatabasePort(queryItemMatchResultOptionToAnswerDetailComRespDto.getDatabasePort());//SimpleFieldAssign//sourceId:1639392_1_64620
                    receptionServiceReq.setDatabaseUserId(queryItemMatchResultOptionToAnswerDetailComRespDto.getDatabaseUserId());//SimpleFieldAssign//sourceId:1639393_1_64620
                    receptionServiceReq.setDatabasePassWord(queryItemMatchResultOptionToAnswerDetailComRespDto.getDatabasePassWord());//SimpleFieldAssign//sourceId:1639394_1_64620
                }

                /*M2接收数据库配置出参[8109]  用于特殊方法接收上游入参。 */
                Assert.isNull(receptionServiceReq.getDatabaseName(), "D2查询数据库配置答案(公共)-M2接收数据库配置出参-数据库名称不能为空", false);
                Assert.isNull(receptionServiceReq.getDatabaseAddress(), "D2查询数据库配置答案(公共)-M2接收数据库配置出参-数据库地址不能为空", false);
                Assert.isNull(receptionServiceReq.getDatabasePort(), "D2查询数据库配置答案(公共)-M2接收数据库配置出参-数据库端口不能为空", false);
                Assert.isNull(receptionServiceReq.getDatabaseUserId(), "D2查询数据库配置答案(公共)-M2接收数据库配置出参-数据库用户名不能为空", false);
                Assert.isNull(receptionServiceReq.getDatabasePassWord(), "D2查询数据库配置答案(公共)-M2接收数据库配置出参-数据库密码不能为空", false);
                receptionServiceRes = nbSystemDocking.implementSmartSessionReceiveFieldInput(receptionServiceReq);


                receptionServiceRes_1 = receptionServiceRes;
            }
        }
        QueryDatabaseConfAnswerDetailComRespDto retData = new QueryDatabaseConfAnswerDetailComRespDto();
        if (receptionServiceRes_1 != null) {
            retData.setDatabaseName(receptionServiceRes_1.getDatabaseName());//SimpleFieldAssign//sourceId:1639396_1
            retData.setDatabaseAddress(receptionServiceRes_1.getDatabaseAddress());//SimpleFieldAssign//sourceId:1639397_1
            retData.setDatabasePort(receptionServiceRes_1.getDatabasePort());//SimpleFieldAssign//sourceId:1639398_1
            retData.setDatabaseUserId(receptionServiceRes_1.getDatabaseUserId());//SimpleFieldAssign//sourceId:1639399_1
            retData.setDatabasePassWord(receptionServiceRes_1.getDatabasePassWord());//SimpleFieldAssign//sourceId:1639400_1
        }


        return retData;
    }

    /**
     * D2执行导入excel文件数据(公共)[8746]
     * gen by moon at 3/23/2024, 8:07:14 PM
     */
    @Trace(operationName = "D2执行导入excel文件数据(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ImplementImportExcelFileDataComRespDto implementImportExcelFileDataCom(ImplementImportExcelFileDataComReqDto reqDto) {


        //步骤0: M2执行导入excel文件数据（特殊方法） - implementImportExcelFileData
        //ModelCode: dataParsingWarehousing
        ImplementImportExcelFileDataRespDto dataParsingWarehousingRes = null;
        ImplementImportExcelFileDataReqDto dataParsingWarehousingReq = new ImplementImportExcelFileDataReqDto();
        if (reqDto != null) {
            dataParsingWarehousingReq.setDatabaseName(reqDto.getDatabaseName());//SimpleFieldAssign//sourceId:1637840_1
            dataParsingWarehousingReq.setDatabaseAddress(reqDto.getDatabaseAddress());//SimpleFieldAssign//sourceId:1637836_1
            dataParsingWarehousingReq.setDatabasePort(reqDto.getDatabasePort());//SimpleFieldAssign//sourceId:1637837_1
            dataParsingWarehousingReq.setDatabaseUserId(reqDto.getDatabaseUserId());//SimpleFieldAssign//sourceId:1637838_1
            dataParsingWarehousingReq.setDatabasePassWord(reqDto.getDatabasePassWord());//SimpleFieldAssign//sourceId:1637839_1
            dataParsingWarehousingReq.setBusinessObjectCode(reqDto.getBusinessObjectCode());//SimpleFieldAssign//sourceId:1637842_1
            dataParsingWarehousingReq.setFileContent(reqDto.getFileContent());//SimpleFieldAssign//sourceId:1637843_1
        }
        if (reqDto != null && reqDto.getBaseMetadataObjectPropertyList() != null && !CollectionUtil.isEmpty(reqDto.getBaseMetadataObjectPropertyList())) {
            dataParsingWarehousingReq.setBaseMetadataObjectPropertyList(reqDto.getBaseMetadataObjectPropertyList().stream().map(item -> BeanUtil.toBean(item, BaseMetadataObjectPropertyDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:1637841_1
        }

        /*M2执行导入excel文件数据（特殊方法）[8747]  入参解析的内容与表名及，根据数据库地址账号密码等，找到对象的表名，解析文件内容转换为数据集，根据入参的内容写入数据库 */
        Assert.isNull(dataParsingWarehousingReq.getDatabaseName(), "D2执行导入excel文件数据(公共)-M2执行导入excel文件数据（特殊方法）-数据库名称不能为空", false);
        Assert.isNull(dataParsingWarehousingReq.getDatabaseAddress(), "D2执行导入excel文件数据(公共)-M2执行导入excel文件数据（特殊方法）-数据库地址不能为空", false);
        Assert.isNull(dataParsingWarehousingReq.getDatabasePort(), "D2执行导入excel文件数据(公共)-M2执行导入excel文件数据（特殊方法）-数据库端口不能为空", false);
        Assert.isNull(dataParsingWarehousingReq.getDatabaseUserId(), "D2执行导入excel文件数据(公共)-M2执行导入excel文件数据（特殊方法）-数据库用户名不能为空", false);
        Assert.isNull(dataParsingWarehousingReq.getDatabasePassWord(), "D2执行导入excel文件数据(公共)-M2执行导入excel文件数据（特殊方法）-数据库密码不能为空", false);
        Assert.isNull(dataParsingWarehousingReq.getBusinessObjectCode(), "D2执行导入excel文件数据(公共)-M2执行导入excel文件数据（特殊方法）-业务对象编码不能为空", false);
        Assert.isNull(dataParsingWarehousingReq.getFileContent(), "D2执行导入excel文件数据(公共)-M2执行导入excel文件数据（特殊方法）-文件内容不能为空", false);
        dataParsingWarehousingRes = nbSystemDocking.implementImportExcelFileData(dataParsingWarehousingReq);


        ImplementImportExcelFileDataComRespDto retData = new ImplementImportExcelFileDataComRespDto();


        return retData;
    }

    /**
     * D2执行Excel导入(公共)[8749]
     * gen by moon at 3/23/2024, 8:07:15 PM
     */
    @Trace(operationName = "D2执行Excel导入(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ImplementExcelImportComRespDto implementExcelImportCom(ImplementExcelImportComReqDto reqDto) {


        ImplementExcelImportRespDto excelImportRes_1 = null;
//步骤0: M2执行Excel导入（特殊方法） - implementExcelImport
        //ModelCode: excelImport
        ImplementExcelImportRespDto excelImportRes = null;
        ImplementExcelImportReqDto excelImportReq = new ImplementExcelImportReqDto();
        if (reqDto != null) {
            excelImportReq.setFile(reqDto.getFile());//SimpleFieldAssign//sourceId:1637798_1
            excelImportReq.setUseType(reqDto.getUseType());//SimpleFieldAssign//sourceId:1637799_1
            excelImportReq.setSpaceId(reqDto.getSpaceId());//SimpleFieldAssign//sourceId:1637800_1
        }

        /*M2执行Excel导入（特殊方法）[8745]  用于系统对接等Excel文件导入 */
        Assert.isNull(excelImportReq.getFile(), "D2执行Excel导入(公共)-M2执行Excel导入（特殊方法）-文件不能为空", false);
        Assert.isNull(excelImportReq.getUseType(), "D2执行Excel导入(公共)-M2执行Excel导入（特殊方法）-文件用途类型不能为空", false);
        Assert.isNull(excelImportReq.getSpaceId(), "D2执行Excel导入(公共)-M2执行Excel导入（特殊方法）-创建于空间ID不能为空", false);
        excelImportRes = nbSystemDocking.implementExcelImport(excelImportReq);


        excelImportRes_1 = excelImportRes;

        ImplementExcelImportComRespDto retData = new ImplementExcelImportComRespDto();
        if (excelImportRes_1 != null) {
            retData.setFileContent(excelImportRes_1.getFileContent());//SimpleFieldAssign//sourceId:1637806_1
            retData.setUnstructuredDataId(excelImportRes_1.getUnstructuredDataId());//SimpleFieldAssign//sourceId:1637807_1
        }


        return retData;
    }
    //
}
